class MoneyType < ActiveRecord::Base
  validates_presence_of :code,
    :name,
    :key_words
  
  validates_uniqueness_of :code,
    :name

  def self.find_by_key_word(key_word)
    first :conditions => [
      ' key_words like ?',
      "%#{key_word}%"
    ]
  end
  
  def self.find_all_by_key_word(key_word)
    all :conditions => [
      ' key_words like ?',
      "%#{key_word}%"
    ]
  end

  def current_fer
    ForeignExchangeRate.find_by_money_type_id( id ,
      :order => 'released_at desc'
    )
  end
  
  def send_sm_to(user)
    user.send_sm_by_lite_fetion to_sm
  end
  
  def to_sm
    if current_fer
      "【#{name}】对〖人民币〗汇率：#{current_fer.spot_rate_buying_rate ? "现汇买入=>#{current_fer.spot_rate_buying_rate}, " : ''}#{current_fer.spot_cash_buying_rate ? "现钞买入=>#{current_fer.spot_cash_buying_rate}, " : ''}#{current_fer.selling_rate ? "卖出=>#{current_fer.selling_rate}, " : ''}#{current_fer.base_price ? "基准价=>#{current_fer.base_price}, " : ''} ==中国银行发布于#{current_fer.released_at.to_s},此汇率仅供参考，以中国银行各分行实际交易汇率为准=="
    else
      "中国银行暂未公布【#{name}】对〖人民币〗的汇率 :_("
    end
  end
end
